$(function() {
	
	freshQuestionList(1);
	
	$('#searchQuestionForm #reservation').daterangepicker({}, function(start, end) {
		var $searchQuestionForm = $('#searchQuestionForm');
		// 确认或者清除
		if(start){
			$searchQuestionForm.find('#starttime').val(start.toString('yyyy-MM-dd'));
			$searchQuestionForm.find('#endtime').val(end.toString('yyyy-MM-dd'));
		}else{
			$searchQuestionForm.find('#starttime').val("");
			$searchQuestionForm.find('#endtime').val("");
		}
	});
	
	$("#searchQuestionBtn").bind("click", function(e){
		freshQuestionList(1);
	});
	
	$("#addQuestionBtn").bind("click", function(e){
		$("#formModel").modal("toggle");
	});
	
	$("#priviewBtn").bind("click", function(e){
		priviewHtml();
	});
	
	$("#closeQuestionFormBtn").bind("click", function(e){
		$('#formModel').modal('hide');
	});
	
	$("#addQuestionFormBtn").bind("click", function(e){
		addQuestionForm();
	});
	
	function addQuestionForm(){
		var $form = $("#addQuestionForm");
		$.ajax({
			type: "post",
			url: $form.attr("action"),
		  	data: $form.serializeArray(),
		  	dataType: "html",
		  	cache: false,
		  	success: function(result){
		  		alert(result);
		  		$('#formModel').modal('hide');
		  		freshQuestionList(1);
		  	},
		  	error: function(){
		  		alert(1);
		  	}
		});
	}
	
	function priviewHtml(){
		$.ajax({
			type: "post",
			url: $("#previewForm").attr("action"),
		  	data: $("#addQuestionForm").serializeArray(),
		  	dataType: "html",
		  	cache: false,
		  	success: function(result){
		  		$("#priview").html(result);
		  	},
		  	error: function(){
		  		alert(1);
		  	}
		});
	}
	
	function freshQuestionList(currentpage){
		var $form = $("#searchQuestionForm");
		console.log($form.attr("action"));
		console.log($form.serializeArray());
		$.ajax({
			type: "post",
			url: $form.attr("action") + "?currentpage=" + currentpage,
		  	data: $form.serializeArray(),
		  	dataType: "json",
		  	cache: false,
		  	beforeSend:function(){
		  		$("#alertContainer #tipContent").html("加载中...");
		  		$("#alertContainer").alert();
		  	},
		  	success: function(result){
		  		console.log(result.currentpage);
		  		console.log(result.totalpage);
		  		console.log(result.list);
		  		
		  		var currentPage = result.currentpage;
		  		var totalPage = result.totalpage;
		  		var data = result.list;
		  		// 表格
		  		var renderDom = $("#questionListTemplate").render(data, {
		  			dateFormat: function(val){
		  				return dateValue2DateStr(val);
		  			}
		  		});
				console.log(renderDom);
				var $questionList = $("#questionList tbody"); // tbody中才能正确渲染
				$questionList.find("tr:gt(0)").remove();
				$questionList.append(renderDom);
		  		// 分页
		  		if(currentPage <= totalPage){
		  			$("#pagination-section").pagination({
			  			current_page : currentPage,
						total_pages : totalPage,
						prev: "«",
						next: "»",
						callback : function(event, page) {
							freshQuestionList(page);
						}
					});
		  		}else{
		  			$("#pagination-section").html("");
		  		}
		  	},
		  	error: function(){
		  		$("#alertContainer #tipContent").html("获取失败!");
		  	},
		  	complete: function(){
		  		$("#alertContainer").alert('close');
		  	}
		});
	}
	
});